package com.six666.medicaldevicetestingsystem.controller;
import com.six666.medicaldevicetestingsystem.dto.*;
import com.six666.medicaldevicetestingsystem.entity.BaseResponse;
import com.six666.medicaldevicetestingsystem.entity.ProjectOfficers;
import com.six666.medicaldevicetestingsystem.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@CrossOrigin
@RestController

public class ProjectOfficers_Controller {
    @Autowired
    private ProjectOfficers_Service projectOfficers_service;
    @Autowired
    private File_Service file_service;
    @Autowired
    ProjectFile_Service project_file_service;
    @PostMapping("/people_msg")
    public BaseResponse<People_DTO>People(@RequestParam Map<String,String> params) {
        int project_id = 0;
        int user_id = 0;
        if (params.get("project_id") != null) {
            try {
                project_id = Integer.parseInt(params.get("project_id"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "project_id非整形");
            }
        }
        if (params.get("user_id") != null) {
            try {
                user_id = Integer.parseInt(params.get("user_id"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "user_id非整形");
            }
        }
        People_DTO data;
        if(projectOfficers_service.People(project_id,user_id)==2) {
            data = new People_DTO(projectOfficers_service.resempty(project_id, user_id), projectOfficers_service.People(project_id, user_id),null);
        }
        else{
            data = new People_DTO(projectOfficers_service.resempty(project_id, user_id), projectOfficers_service.People(project_id, user_id), projectOfficers_service.PeopleDate(project_id,user_id));
        }
        return new BaseResponse<>(data);
    }
    @PostMapping("/upload_c_msg")
    public BaseResponse<Boolean>add_c_task(@RequestParam("file") MultipartFile file,@RequestParam Map<String,String> params){
        if (file.isEmpty()) {
            return new BaseResponse<>(101,null,"参数错误","文件为空");
        }
        int project_id = 0;
        if (params.get("project_id") != null) {
            try {
                project_id = Integer.parseInt(params.get("project_id"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "project_id非整形");
            }
        }
        String fileName = "TrainVideo#" + file.getOriginalFilename();
        file_service.save_file(file, "train_video", fileName);
        boolean res;
        project_file_service.delete_file_by_project_id_and_type(project_id,"TrainVideo");
        res=project_file_service.add_project_file(project_id,"TrainVideo","train_video/"+fileName,fileName);
        return new BaseResponse<>(res);

    }
    @PostMapping("/upload_t_msg")
    public BaseResponse<Boolean>add_t_task(@RequestParam("file") MultipartFile file,@RequestParam Map<String,String> params){
        if (file.isEmpty()) {
            return new BaseResponse<>(101,null,"参数错误","文件为空");
        }
        int project_id = 0;
        if (params.get("project_id") != null) {
            try {
                project_id = Integer.parseInt(params.get("project_id"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "project_id非整形");
            }
        }
        String fileName = "Paper#"+ file.getOriginalFilename();
        file_service.save_file(file, "paper", fileName);
        boolean res;
        project_file_service.delete_file_by_project_id_and_type(project_id,"Paper");
        res=project_file_service.add_project_file(project_id,"Paper","paper/"+fileName,fileName);
        return new BaseResponse<>(res);

    }
    @PostMapping("/observe_progress")
    public BaseResponse<List<Observe_DTO>>observe_progress(@RequestParam Map<String,String> params){
        int user_id;
        try {
            user_id = Integer.parseInt(params.get("user_id"));
        } catch (Exception e) {
            user_id = 0;
        }
        int project_id = 0;
        if (params.get("project_id") != null) {
            try {
                project_id = Integer.parseInt(params.get("project_id"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "project_id非整形");
            }
        }
        List<Observe_DTO> observe;
        observe = projectOfficers_service.getObserve(project_id,user_id);
        return new BaseResponse<>(observe);
    }

    @PostMapping("/people_task_1_msg")
    public BaseResponse<Boolean>task_1(@RequestParam Map<String,String> params,@RequestParam("file") MultipartFile file){
        int project_id = 0;
        int user_id = 0;
        if (params.get("project_id") != null) {
            try {
                project_id = Integer.parseInt(params.get("project_id"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "project_id非整形");
            }
        }
        if (params.get("user_id") != null) {
            try {
                user_id = Integer.parseInt(params.get("user_id"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "user_id非整形");
            }
        }
        if (file.isEmpty()) {
            return new BaseResponse<>(101,null,"参数错误","文件为空");
        }
        String fileName = "task" +user_id+ "_" + "1#"+ file.getOriginalFilename();
        file_service.save_file(file, "a_paper", fileName);
        project_file_service.delete_file_by_project_id_and_type_plus(project_id,"AssessmentPaper","a_paper/"+fileName,fileName);
        project_file_service.add_project_file(project_id,"AssessmentPaper","a_paper/"+fileName,fileName);
        int file_id = project_file_service.get_file_id_by_project_id_and_type_and_filename(project_id,"AssessmentPaper",fileName);
        boolean res = projectOfficers_service.updateAssessmentPaper(project_id,user_id,file_id);
        return new BaseResponse<>(res);
    }
    @PostMapping("/people_task_2_msg")
    public BaseResponse<Boolean>task_2(@RequestParam Map<String,String> params,@RequestParam("file") MultipartFile file){
        int project_id = 0;
        int user_id = 0;
        if (params.get("project_id") != null) {
            try {
                project_id = Integer.parseInt(params.get("project_id"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "project_id非整形");
            }
        }
        if (params.get("user_id") != null) {
            try {
                user_id = Integer.parseInt(params.get("user_id"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "user_id非整形");
            }
        }
        if (file.isEmpty()) {
            return new BaseResponse<>(101,null,"参数错误","文件为空");
        }
        String fileName = "task" +user_id+ "_" + "2#"+ file.getOriginalFilename();
        file_service.save_file(file, "a_video", fileName);
        project_file_service.delete_file_by_project_id_and_type_plus(project_id,"AssessmentVideo","a_video/"+fileName,fileName);
        project_file_service.add_project_file(project_id,"AssessmentVideo","a_video/"+fileName,fileName);
        int file_id = project_file_service.get_file_id_by_project_id_and_type_and_filename(project_id,"AssessmentVideo",fileName);
        boolean res = projectOfficers_service.updateAssessmentVideo(project_id,user_id,file_id);
        return new BaseResponse<>(res);
    }
    @PostMapping("/get_task_1_msg")
    public BaseResponse<List<String>>task_1(@RequestParam Map<String,String> params){
        int project_id = 0;
        if (params.get("project_id") != null) {
            try {
                project_id = Integer.parseInt(params.get("project_id"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "project_id非整形");
            }
        }
        return new BaseResponse<>(projectOfficers_service.getPaper(project_id));
    }
    @PostMapping("/authorize_msg")
    public BaseResponse<List<Authorize_DTO>>authorize(@RequestParam Map<String,String> params){
        int project_id = 0;
        if (params.get("project_id") != null) {
            try {
                project_id = Integer.parseInt(params.get("project_id"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "project_id非整形");
            }
        }
        List<ProjectOfficers> res = projectOfficers_service.getauthorize(project_id);
        List<Authorize_DTO> authorizeDtos = new ArrayList<>();
        for (ProjectOfficers re : res) {
            Authorize_DTO dto = new Authorize_DTO(re.getAccountid(), re.getExamination());
            authorizeDtos.add(dto);
        }
        return new BaseResponse<>(authorizeDtos) ;
    }


    @PostMapping("/task_msg")
    public BaseResponse<task_DTO>task_msg(@RequestParam Map<String,String> params){
        int project_id = 0;
        int user_id = 0;
        if (params.get("project_id") != null) {
            try {
                project_id = Integer.parseInt(params.get("project_id"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "project_id非整形");
            }
        }
        if (params.get("user_id") != null) {
            try {
                user_id = Integer.parseInt(params.get("user_id"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "user_id非整形");
            }
        }
        ProjectOfficers res = projectOfficers_service.gettask(project_id,user_id);
        task_DTO dto = new task_DTO();
        dto.setProgress(res.getTraining());
        dto.setTask_1(res.getAssessmentpaperstatus());
        dto.setTask_2(res.getAssessmentvideostatus());
        dto.setIs_autho(res.getExamination());
        if(res.getExamination().equals("Finish")){
            dto.setAuth_time(res.getExaminationendtime());
        }
        else{
            dto.setAuth_time(null);
        }
        return new BaseResponse<>(dto);


    }
    @PostMapping("/download_c_msg")
    public BaseResponse<List<String>>download_c_msg(@RequestParam Map<String,String> params){
        int project_id = 0;
        if (params.get("project_id") != null) {
            try {
                project_id = Integer.parseInt(params.get("project_id"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "project_id非整形");
            }
        }
        return new BaseResponse<>(projectOfficers_service.getTrainVideo(project_id));
    }
    @PostMapping("/change_msg")
    public BaseResponse<Boolean>change_msg(@RequestParam Map<String,String> params){
        int project_id = 0;
        if (params.get("project_id") != null) {
            try {
                project_id = Integer.parseInt(params.get("project_id"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "project_id非整形");
            }
        }
        int user_id = 0;
        if (params.get("user_id") != null) {
            try {
                user_id = Integer.parseInt(params.get("user_id"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "user_id非整形");
            }
        }
        return new BaseResponse<>(projectOfficers_service.change(project_id,user_id));

    }
    @PostMapping("/change_1_msg")
    public BaseResponse<Boolean>change_1_msg(@RequestParam Map<String,String> params){
        int project_id = 0;
        if (params.get("project_id") != null) {
            try {
                project_id = Integer.parseInt(params.get("project_id"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "project_id非整形");
            }
        }
        int user_id = 0;
        if (params.get("user_id") != null) {
            try {
                user_id = Integer.parseInt(params.get("user_id"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "user_id非整形");
            }
        }
        return new BaseResponse<>(projectOfficers_service.change_1(project_id,user_id));

    }
        @PostMapping("/change_2_msg")
    public BaseResponse<Boolean>change_2_msg(@RequestParam Map<String,String> params){
        int project_id = 0;
        if (params.get("project_id") != null) {
            try {
                project_id = Integer.parseInt(params.get("project_id"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "project_id非整形");
            }
        }
        int user_id = 0;
        if (params.get("user_id") != null) {
            try {
                user_id = Integer.parseInt(params.get("user_id"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "user_id非整形");
            }
        }
        int state = 0;
        if (params.get("state") != null) {
            try {
                state = Integer.parseInt(params.get("state"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "state非整形");
            }
        }
        if(state ==0 ){
            return new BaseResponse<>(projectOfficers_service.change_p_0(project_id,user_id));
        }
        else if(state ==1 ){
            return new BaseResponse<>(projectOfficers_service.change_p_1(project_id,user_id));
        }
        else if(state ==2 ){
            return new BaseResponse<>(projectOfficers_service.change_p_2(project_id,user_id));
        }
        else if(state ==3 ){
            return new BaseResponse<>(projectOfficers_service.change_p_3(project_id,user_id));
        }
        else {
            return new BaseResponse<>(103, null, "参数取值范围错误", "state值出错");
        }
    }
    @PostMapping("/change_3_msg")
    public BaseResponse<Boolean>change_3_msg(@RequestParam Map<String,String> params){
        int project_id = 0;
        if (params.get("project_id") != null) {
            try {
                project_id = Integer.parseInt(params.get("project_id"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "project_id非整形");
            }
        }
        int user_id = 0;
        if (params.get("user_id") != null) {
            try {
                user_id = Integer.parseInt(params.get("user_id"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "user_id非整形");
            }
        }
        int state = 0;
        if (params.get("state") != null) {
            try {
                state = Integer.parseInt(params.get("state"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "state非整形");
            }
        }
        if( state==0 ){
            return new BaseResponse<>(projectOfficers_service.change_v_0(project_id,user_id));
        }
        else if(state==1 ){
            return new BaseResponse<>(projectOfficers_service.change_v_1(project_id,user_id));
        }
        else if(state==2 ){
            return new BaseResponse<>(projectOfficers_service.change_v_2(project_id,user_id));
        }
        else if(state==3 ){
            return new BaseResponse<>(projectOfficers_service.change_v_3(project_id,user_id));
        }
        else {
            return new BaseResponse<>(103, null, "参数取值范围错误", "state值出错");
        }
    }
    @PostMapping("/change_4_msg")
    public BaseResponse<Boolean>change_4_msg(@RequestParam Map<String,String> params){
        int project_id = 0;
        if (params.get("project_id") != null) {
            try {
                project_id = Integer.parseInt(params.get("project_id"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "project_id非整形");
            }
        }
        int user_id = 0;
        if (params.get("user_id") != null) {
            try {
                user_id = Integer.parseInt(params.get("user_id"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "user_id非整形");
            }
        }
        return new BaseResponse<>(projectOfficers_service.change_2(project_id,user_id));
    }
    @PostMapping("/get_write_msg")
    public BaseResponse<String>get_write_msg(@RequestParam Map<String,String> params){
        int project_id = 0;
        if (params.get("project_id") != null) {
            try {
                project_id = Integer.parseInt(params.get("project_id"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "project_id非整形");
            }
        }
        int user_id = 0;
        if (params.get("user_id") != null) {
            try {
                user_id = Integer.parseInt(params.get("user_id"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "user_id非整形");
            }
        }
        return new BaseResponse<>(projectOfficers_service.get_write_msg(project_id,user_id));
    }
    @PostMapping("/get_video_msg")
    public BaseResponse<String>get_video_msg(@RequestParam Map<String,String> params){
        int project_id = 0;
        if (params.get("project_id") != null) {
            try {
                project_id = Integer.parseInt(params.get("project_id"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "project_id非整形");
            }
        }
        int user_id = 0;
        if (params.get("user_id") != null) {
            try {
                user_id = Integer.parseInt(params.get("user_id"));
            } catch (Exception e) {
                return new BaseResponse<>(102, null, "参数错误", "user_id非整形");
            }
        }
        return new BaseResponse<>(projectOfficers_service.get_video_msg(project_id,user_id));
    }

    @PostMapping("/get_c_by_user_id")
    public BaseResponse<List<Observe_2_DTO>>get_c_by_user_id(@RequestParam Map<String,String> params){
        int user_id = 0;
        if (params.get("user_id") != null) {
            user_id = Integer.parseInt(params.get("user_id"));}
        else {
            user_id = 0;
        }
        List<Observe_DTO> observe;
        return new BaseResponse<>(projectOfficers_service.get_observe_2(user_id));

    }
//    public BaseResponse<Authorize_DTO>Authorize(@RequestParam Map<String,String> params) {
//
//        data = new Authorize_DTO();
//        return BaseResponse<>(data);
//    }
}
